#ifndef SXCOM_H
#define SXCOM_H

#include <iostream>
#include <wiringSerial.h>
#include <sys/types.h>		// For POSIX datatypes
#include <stdint.h>		// For POSIX datatypes
#include <openssl/md4.h>	// For hashing
#include <string.h>
#include <unistd.h>

#define BAUD_RATE 57600 
#define SER_PORT "/dev/ttyUSB0"
#define DIGEST_SIZE 16
#define MAX_MSG_SIZE 0xFF	// Max size of a message
#define CALL_CARD 0x55		// ID of a message
#define HEADER_SIZE 6

struct xMsg{
	uint8_t callCard;	// Signifies start of a message
	uint8_t msgNum;		// Numeric ID of the message (used to guard against duplicates)	
	uint8_t toAddr;		// Address of sender
	uint8_t fromAddr;	// Address of receiver
	uint8_t size;		// Size of message body
	uint8_t confirmation;	// Used like boolean
	uint8_t headerDigest[DIGEST_SIZE];	// Digest of msgNum, tofrom addresses, size and confirmation
	uint8_t messageBody[MAX_MSG_SIZE];	// Body of the message 
	uint8_t messageBodyDigest[DIGEST_SIZE];	// Digest of the message body
};

typedef xMsg xMsg; 

extern int fd;		// File descriptor for wiringSerial
extern uint8_t CUBEID;	// The local cube ID

int initSXCOM(uint8_t ID);

xMsg * createMessage(uint8_t num, uint8_t to, uint8_t from, uint8_t size, uint8_t * data);

xMsg * createGoodConfirmation(uint8_t to, uint8_t from);

xMsg * createBadConfirmation(uint8_t to, uint8_t from);

int msgSend(xMsg * msg);


bool goodSend();


uint8_t * msgRec(uint8_t * size);

#endif
